
In networking jargon, a "connection" (or "tcp connection") is
thought of like this


                       a tcp connection
    IP address O-------------------------------O IP address
   Port number                                   Port number


A "connection" is a pair of "endpoints" and each endpoint is
an ip:port address pair. (So a connection is a pair of pairs.)


At each endpoint of a connection there is a socket.

       socket                                         socket
   +------------+                                 +------------+
   |            |        a tcp connection         |            |
   | IP address |O-------------------------------O| IP address |
   |Port number |                                 | Port number|
   |            |                                 |            |
   |            |                                 |            |
   +------------+                                 +------------+

 Each socket is "bound" to its end of the connection and
 is "connected" to the other end of the connection.


Know these terms:
   connection
   endpoint
   bind
   connect




Here are the steps used by a tcp server and client to communicate
with each other.


Step 1) The tcp server process creates an unbound ServerSocket.
        This ServerSocket is not yet able to establish connections
        with clients.

                                                   ServerSocket
                                                  +------------+
                                                  |            |
                                                 O| IP address |
                                                  |            |
                                                  |            |
                                                  |            |
                                                  +------------+


Step 2) The tcp server process binds its ServerSocket to a port number.
        This ServerSocket is not yet able to establish connections
        with clients.

                                                   ServerSocket
                                                  +------------+
                                                  |            |
                                                 O| IP address |
                                                  | Port number|
                                                  |            |
                                                  |            |
                                                  +------------+


Step 3) The tcp server process calls accept() on its ServerSocket.
        The ServerSocket can now establish connections with
        clients.


Step 4) A tcp client process creates a (unbound) Socket.

       Socket                                      ServerSocket
   +------------+                                 +------------+
   |            |                                 |            |
   | IP address |O                               O| IP address |
   |            |                                 | Port number|
   |            |                                 |            |
   |            |                                 |            |
   +------------+                                 +------------+


Step 5) The client process asks for Socket to (bind and) connect
        to the ServerSocket. (This picture would be just before 
        the accept() function returns.)

       Socket                                      ServerSocket
   +------------+                                 +------------+
   |            |       tcp connection            |            |
   | IP address |O-------------------------------O| IP address |
   |Port number |                                 | Port number|
   |            |                                 |            |
   |            |                                 |            |
   +------------+                                 +------------+


 Step 6) After the connection is established, the ServerSocket
         creates a new communicating Socket and connects the
         client Socket to the new Socket. (This picture would 
         be just after the accept() function returns.)

       Socket                                      ServerSocket
   +------------+                                 +------------+
   |            |       tcp connection            |            |
   | IP address |O-------------------------+     O| IP address |
   |Port number |                          |      | Port number|
   |            |                          |      |            |
   |            |                          |      |            |
   +------------+                          |      +------------+
                                           |
                                           |          Socket
                                           |      +------------+
                                           |      |            |
                                           +-----O| IP address |
                                                  | Port number|
                                                  |            |
                                                  |            |
                                                  +------------+


Step 6a) If the server process is parallelized, then the server
         can call accept() once again on the ServerSocket and
         establish a simultaneous connection with a second client.
         If the server process is not parallelized, then the
         ServerSocket cannot establish a new client connection
         while the server process is communicating with the
         current client. (But new clients can queue up on the
         blocked ServerSocket!)


Step 7) The client and server processes get input and output
        streams from their communicating Socket objects.

       Socket                                      ServerSocket
   +------------+                                 +------------+
   |            |       tcp connection            |            |
   | IP address |O-------------------------+     O| IP address |
   |Port number |                          |      | Port number|
   |            |                          |      |            |
   |            |                          |      |            |
   +------------+                          |      +------------+
      /\   \/                              |
      |     |                              |          Socket
      |     |                              |      +------------+
    out     in                             |      |            |
                                           +-----O| IP address |
                                                  | Port number|
                                                  |            |
                                                  |            |
                                                  +------------+
                                                     /\   \/
                                                     |    |
                                                     |    |
                                                   out    in


Step 8) The client and server processes implement an application
        level protocol using the tcp connection and their associated
        streams.


Step 9) Either the client or the server process closes its end of
        the tcp connection (by calling close() on its Socket). When
        the other endpoint detects this, the other endpoint closes
        its end of the connection (by calling close() on its Socket).

       Socket                                      ServerSocket
   +------------+                                 +------------+
   |            |                                 |            |
   | IP address |O                               O| IP address |
   |Port number |                                 | Port number|
   |            |                                 |            |
   |            |                                 |            |
   +------------+                                 +------------+
      /\   \/
      |     |                                         Socket
      |     |                                     +------------+
    out     in                                    |            |
                                                 O| IP address |
                                                  | Port number|
                                                  |            |
                                                  |            |
                                                  +------------+
                                                     /\   \/
                                                     |    |
                                                     |    |
                                                   out    in


Step 9) The unused Socket objects get garbage collected.


Step 10) The server process once again calls accept() on its
         ServerSocket and is ready to establish a new connection
         with a client (that is, go to Step 3).

                                                   ServerSocket
                                                  +------------+
                                                  |            |
                                                 O| IP address |
                                                  | Port number|
                                                  |            |
                                                  |            |
                                                  +------------+